package org.jmc.refmanager.web.wicket.page.user;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.jmc.refmanager.domain.model.User;
import org.jmc.refmanager.web.wicket.page.admin.SortableUserDataProvider;

/**
 * 
 */
@AuthorizeInstantiation("ROLE_ADMIN")
public class UsersList extends WebPage {

	public UsersList() {
		super();
		add(new Label("users.pageTitle", "Administration des utilisateurs"));
		
		SortableUserDataProvider dp = new SortableUserDataProvider(null);
		final DataView<User> dataView = new DataView<User>("sorting", dp) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final Item<User> item) {
				User user = item.getModelObject();
//				item.add(new ActionPanel("actions", item.getModel()));
				item.add(new Label("matricule", user.getMatricule()));
				item.add(new Label("enabled", String.valueOf(user.isEnabled())));
//				Collection<GrantedAuthority> authz = user.getAuthorities();
//				item.add(new Label("roleName", authz.toString()));

				item.add(new AttributeModifier("class", true,
						new AbstractReadOnlyModel<String>() {
							private static final long serialVersionUID = 1L;

							@Override
							public String getObject() {
								return (item.getIndex() % 2 == 1) ? "even"
										: "odd";
							}
						}));
			}
		};
		
		dataView.setItemsPerPage(10);
		
		add(new OrderByBorder("orderByMatricule", "matricule", dp) {
            @Override
            protected void onSortChanged() {
                dataView.setCurrentPage(0);
            }
        });

//		add(new OrderByBorder("orderByRoleName", "roleName", dp) {
//            @Override
//            protected void onSortChanged() {
//                dataView.setCurrentPage(0);
//            }
//        });
		
		add(dataView);
		
//		add(new PagingNavigator("navigator", dataView));

	}

}

